package servlet.pack1010;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/1010/demoa")
public class DemoAServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pass = request.getParameter("pass");
        String is = request.getParameter("is");
        if (null==is){
            Cookie cookie1 = new Cookie("USER_NAME",name);
            cookie1.setMaxAge(60*60*24);
            response.addCookie(cookie1);
            Cookie cookie2 = new Cookie("USER_PASS",pass);
            cookie2.setMaxAge(60*60*24);
            response.addCookie(cookie2);
        }
        response.sendRedirect("index.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie1 = new Cookie("USER_NAME","name");
        cookie1.setMaxAge(0);
        response.addCookie(cookie1);
        try {
            Thread.sleep(1200);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        response.sendRedirect("login.jsp");
    }
}
